package com.itheima.reggie.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.itheima.reggie.commom.EmployeeHolder;
import com.itheima.reggie.commom.UserHolder;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Sea
 * @date 2023/3/4 16:54
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
//    @Override
//    //插入操作 自动填充
//    public void insertFill(MetaObject metaObject) {
//        metaObject.setValue("createTime",new Date());
//        metaObject.setValue("updateTime",new Date());
//        metaObject.setValue("createUser",1L);
//        metaObject.setValue("updateUser",1L);
//    }
//    //更新操作 自动填充
//    @Override
//    public void updateFill(MetaObject metaObject) {
//       metaObject.setValue("updateTime",new Date());
//       metaObject.setValue("updateUser",1L);
//    }


    //插入操作，自动填充
//    @Override
//    public void insertFill(MetaObject metaObject) {
//        metaObject.setValue("createTime", new Date());
//        metaObject.setValue("updateTime", new Date());
//
//        Employee employee = EmployeeHolder.get();
//        // 一定要做非空判断
//        if (employee != null) {
//            metaObject.setValue("createUser", employee.getId());
//            metaObject.setValue("updateUser", employee.getId());
//        }
//
//    }
//
//    //更新操作，自动填充
//    @Override
//    public void updateFill(MetaObject metaObject) {
//        metaObject.setValue("updateTime", new Date());
//
//        Employee employee = EmployeeHolder.get();
//        // 一定要做非空判断
//        if (employee != null) {
//            metaObject.setValue("updateUser", employee.getId());
//        }
//    }
        //插入操作，自动填充
        @Override
        public void insertFill (MetaObject metaObject){
            metaObject.setValue("createTime", new Date());
            metaObject.setValue("updateTime", new Date());
            Employee employee = EmployeeHolder.get();
            if (employee != null) {
                metaObject.setValue("createUser", employee.getId());
                metaObject.setValue("updateUser", employee.getId());
            }

            User user = UserHolder.get();
            if (user != null) {
                metaObject.setValue("createUser", user.getId());
                metaObject.setValue("updateUser", user.getId());
            }
        }

        //更新操作，自动填充
        @Override
        public void updateFill (MetaObject metaObject){
            metaObject.setValue("updateTime", new Date());
            Employee employee = EmployeeHolder.get();
            if (employee != null) {
                metaObject.setValue("updateUser", employee.getId());
            }
            User user = UserHolder.get();
            if (user != null) {
                metaObject.setValue("updateUser", user.getId());
            }
        }
    }


